<template>
    <el-table
        ref="tableRef"
        :data="tableData"
        v-bind="tableDefaultAttribute"
        row-key="id"
        :max-height="tableHeight"
        table-layout="auto"
    >
        <template #empty>
            <span>无数据</span>
        </template>
        <el-table-column type="selection" width="55" :align="'center'" />
        <template v-for="col in tableColumns" :key="col.prop">
            <el-table-column
                v-if="!col.isHidden"
                :align="col.align"
                :prop="col.prop"
                :label="col.label"
                :formatter="col.formatter"
                :type="col.type"
                :fixed="col.fixed"
                :width="col.width"
            >
                <template
                    v-if="col.formFieldItemType === 'boolean'"
                    #default="scope"
                >
                    <el-switch
                        v-model="scope.row[`${col.prop}`]"
                        :active-value="'Y'"
                        :inactive-value="'N'"
                    />
                </template>
                <template
                    v-else-if="col.formFieldItemType === 'text'"
                    #default="scope"
                >
                    <el-input 
                        v-model="scope.row[`${col.prop}`]"
                        
                        type="textarea"
                        disabled
                    />
                </template>
                <template
                    v-else-if="col.formFieldItemType === 'ref'"
                    #default="scope"
                >
                <el-link :href="scope.row[`${col.prop}`]" target="_blank">下载</el-link>
                </template>
            </el-table-column>
        </template>
    </el-table>
    <el-button-group class="bg">
        <el-space>
            
            <el-button
                class="btn"
                type="primary"
                :icon="Search"
                @click="onQuery"
                
                >查询</el-button
            >
            <el-button
                class="btn"
                type="primary"
                :icon="Refresh"
                @click="onRefresh"
                
                >刷新</el-button
            >
            
        </el-space>
    </el-button-group>
    <div class="page">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[50, 100, 150, 200]"
            layout="sizes, prev, pager, next"
            :total="totalnum"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
    
    
    <el-dialog
        v-model="dialogQueryVisible"
        title="进场计划查询"
        draggable
        destroy-on-close
    >
        <QueryForm
            :queryItems="queryItems"
            :tableID="tableRef!.$el.id"
            @yh-closedialog="onCloseDialog"
        ></QueryForm>
    </el-dialog>
</template>
<script setup lang="ts" name="ReleaseForms">
import { ref, reactive, onMounted } from 'vue';
import { storeToRefs } from 'pinia';
import { ElTable } from 'element-plus';
import { Delete, Refresh, Search } from '@element-plus/icons-vue';
import {
    isEmpty,
} from 'lodash';

import {
    tableDefaultAttribute,
    useTableInfo,
    tableColumnDefault,
} from '@/hooks/useTable';
import type { QueryItem, TableColumn } from '@/common/types/app';
import { Request } from '@/app/request';
import {
    pickQueryFromTable,
} from '@/common/tools';
import { tableMaxHeightAdjust } from '@/common/const';
import type {
    TableInstance,
} from 'element-plus';

import { useAppStore } from '@/store/app';
import { transFormatter, formatDate } from '@/common/tools';
import InPlan from './InPlan.vue';
import { ReleaseFormModel } from '@/common/types/container';
const { cntrDam, cyCfs } = storeToRefs(useAppStore());
const tableRef = ref<TableInstance>();
const tableData = ref<Array<ReleaseFormModel>>([]);
const tableHeight = ref(100);
const onFormatter = (_row: any, column: any, cellValue: any) => {
    let data: { value: string | number; label: string }[];
    if (column['property'] === 'dam_id') {
        data = cntrDam.value;
    } else if (column['property'] === 'cfs_cod') {
        data = cyCfs.value;
    } else {
        return cellValue;
    }

    return transFormatter(cellValue, data);
};
const tableColumns = reactive<TableColumn[]>([
    {
        ...tableColumnDefault,
        order: 1,
        prop: 'ReleaseFormModel',
        label: '放箱号',
        formFieldItemType: 'input',
        width: 100,
    },
    
    {
        ...tableColumnDefault,
        order: 2,
        prop: 'create_tim',
        label: '生成放箱单日期',
        formatter: formatDate,
        formFieldItemType: 'date',
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 10,
        prop: 'cfs_cod',
        label: '站点',
        sortable: 'custom',
        formatter: onFormatter,
        formFieldItemType: 'single',
        optionalValues: cyCfs.value,
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 5,
        prop: 'cntr_siz_cod',
        label: '箱尺寸',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 6,
        prop: 'cntr_typ_cod',
        label: '箱型',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 12,
        prop: 'dam_id',
        label: '箱况',
        sortable: 'custom',
        formatter: onFormatter,
        formFieldItemType: 'single',
        optionalValues: cntrDam.value,
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 7,
        prop: 'cntr_num',
        label: '箱量',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 11,
        prop: 'cntr_info',
        label: '箱号信息',
        sortable: 'custom',
        formFieldItemType: 'text',
        width: 200,
    },    
    
    {
        ...tableColumnDefault,
        order: 11,
        prop: 'oper_nam',
        label: '操作员',
        sortable: 'custom',
        width: 200,
    },
    
    {
        ...tableColumnDefault,
        order: 11,
        prop: 'filepath',
        label: '下载',
        sortable: 'custom',
        formFieldItemType: 'ref',
        width: 200,
    },
]);


const dialogQueryVisible = ref(false);
let queryWhere = {};

const onRefresh = async () => {
    const req = new Request('放箱单查询', {
        skip: (currentPage.value - 1) * pageSize.value,
        take: pageSize.value,
        order: orders,
        ...queryWhere,
    });
    const r = await req.post();

    totalnum.value = r.ext;
    tableData.value.length = 0;

    if (!isEmpty(r.obj)) {
        tableData.value = r.obj;
    }
};
const {
    currentPage,
    orders,
    pageSize,
    totalnum,
    handleSizeChange,
    handleCurrentChange,
    handleSortChange,
} = useTableInfo(onRefresh);
onMounted(() => {
    // onRefresh();
    tableHeight.value =
        window.innerHeight -
        Math.ceil(tableRef.value?.$el.getBoundingClientRect().bottom) +
        tableMaxHeightAdjust;
    // alert(tableHeight.value);
});

const onCloseDialog = async (arg: any) => {
    if (dialogQueryVisible.value) {
        // 查询
        currentPage.value = 1;
        queryWhere = arg;
        onRefresh();

        dialogQueryVisible.value = false;
    }
};

const onQuery = () => {
    dialogQueryVisible.value = true;
};
const queryItems: Array<QueryItem> = pickQueryFromTable(tableColumns);

</script>
<style lang="scss" scoped>
.bg {
    margin-top: 10px;

    .btn {
        margin-right: 10px;
    }
}

.page {
    float: right;
    margin-top: 10px;
}
</style>
